// Created by Joe on 2017/7/21.

app.service(
    'RequestProxyService',
    [
        'config', '$http', '$q', 'AccessTokenService', 'UserService',
        function (config, $http, $q, AccessTokenService, UserService) {
            var tokenAwait = function (callback) {
                if (UserService.accessToken) {
                    callback();
                } else {
                    AccessTokenService.getAccessToken()
                        .then(function () {
                            callback();
                        });
                }
            };

            return {
                /**
                 * @url: interface address
                 * @heads: add-on heads
                 * @params: payload param
                 * @type: user/UserServer, other/DataServer
                 * @error: if true, return error info
                 * @method: request method, POST default
                 */
                requestProxy: function (url, heads, params, callback, type, error, method) {
                    var server = type === 'user' ? config.UserServer : config.DataServer;
                    return function () {
                        var deferred = $q.defer();
                        tokenAwait(function () {
                            var mergeHeads = {
                                access_token: UserService.accessToken,
                                build: config.Build,
                                version: config.Version,
                                cityCode: config.curCity.cityCode
                            };

                            for (var h in heads) {
                                if (heads.hasOwnProperty(h)) {
                                    mergeHeads[h] = heads[h];
                                }
                            }

                            method = method || 'POST';
                            $http({
                                method: method,
                                url: server + url,
                                headers: mergeHeads,
                                params: params,
                                cache: true
                            }).success(function (info) {
                                callback(deferred.resolve, info);
                            }).error(function (err) {
                                if (error) callback(deferred.resolve, err);
                                console.log(err);
                            });

                            // var payload = new FormData();
                            // for (var p in params) {
                            //     if (params.hasOwnProperty(p)) {
                            //         payload.append(p, params[p]);
                            //     }
                            // }

                            // $http.post(server + url, {}, {
                            //     headers: mergeHeads,
                            //     params: params
                            // }).success(function (info) {
                            //     callback(deferred.resolve, info);
                            // }).error(function (error) {
                            //     if (error) {
                            //         callback(deferred.resolve, error);
                            //     }
                            //     console.log(error);
                            // });
                        });

                        return deferred.promise;
                    };
                }
            }
        }
    ]
);